package com.lhz.mail;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: LiHuaZhi
 * @Date: 2022/2/17 10:16
 * @Description:
 **/
@RestController
@RequestMapping("/mail")
public class Controller {
    @Resource
    private MailService mailService;
    /**
     * 发送方邮件
     *
     * @return
     */
    @Value("${spring.mail.username}")
    private String sender;

    /**
     * 发送文本邮件
     *
     * @return
     */
    @GetMapping("/test")
    public String test() {
        return "访问成功";
    }

    /**
     * 发送文本邮件
     *
     * @return
     */
    @GetMapping("/sendText")
    public String sendText() {
        //这个是接受人的邮箱
        String receiver = "1071535576@qq.com";
        //标题
        String subject = "邮箱标题(主题)";
        //内容
        String text = "这个是邮箱内容";
        //文本邮箱
        return mailService.sendTestMail(sender, receiver, subject, text);
    }

    /**
     * html模板邮箱
     *
     * @return
     */
    @GetMapping("/sendHtml")
    public String sendHtml() {
        //这个是接受人的邮箱
        String receiver = "1071535576@qq.com";
        //标题
        String subject = "邮箱标题(主题)";
        //内容
        String content = "<html>\n" +
                "<body>\n" +
                "<h2>这是一封有历史意义的HTML邮件,请注意查收!!!</h2>\n" +
                "</body>\n" +
                "</html>";
        //模板邮箱
        return mailService.sendHtml(sender, receiver, subject, content);
    }

    /**
     * 附件邮箱
     *
     * @return
     */
    @GetMapping("/sendAttachment")
    public String sendAttachment() {
        //这个是接受人的邮箱
        String receiver = "1071535576@qq.com";
        //标题
        String subject = "邮箱标题(主题)";
        // 附件位置
        String filePath = "C:\\Users\\LiGezZ\\Desktop\\Java整合Mail.txt";
        String content = "我的这封邮件可以带附件!!!";
        //附件邮箱
        return mailService.sendAttachment(sender, receiver, subject, content, filePath);
    }

    /**
     * 图片邮箱
     *
     * @return
     */
    @GetMapping("/sendPicture")
    public String sendPicture() {
        //这个是接受人的邮箱
        String receiver = "1071535576@qq.com";
        //标题
        String subject = "邮箱标题(主题)";
        String picPath = "C:\\Users\\LiGezZ\\Desktop\\test.jpeg";
        String picId = "PIC001";
        String content = "<html><body>图片邮件\n" +
                "<img src=\'cid:" + picId + "\'></img></body></html>";
        //图片邮箱
        return mailService.sendPicture(sender, receiver, subject, content, picPath, picId);
    }

    /**
     * Thymeleaf+html模板邮箱
     *
     * @return
     */
    @GetMapping("/sendThymeleaf")
    public String sendThymeleaf() {
        //这个是接受人的邮箱
        String receiver = "1071535576@qq.com";
        //标题
        String subject = "邮箱标题(主题)";

        // Thymeleaf+html模板邮箱
        return mailService.sendThymeleaf(sender, receiver, subject);
    }
}
